//
//  LWZFMRootViewControllerW.m
//  MusicFM
//
//  Created by 王景欣 on 16/1/11.
//  Copyright © 2016年 LWZ. All rights reserved.
//

#import "LWZFMRootViewControllerW.h"

@interface LWZFMRootViewControllerW ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSourceShow;

@property (nonatomic, strong) NSMutableArray *dataSourceHost;

@property (nonatomic, strong) NSMutableArray *dataSourceFocusImages;

@property (nonatomic, strong) UIButton *titleButton;

@property (nonatomic, strong) UIImageView *backImageView;

@property (nonatomic , strong) LWZFmListDatasModel *listDatasModelShowModel;

@property (nonatomic, strong) LWZFmListDatasModel *listDatasModelHostModel;

@property (nonatomic, strong) LWZFmFocusImagesListModel *focusImagesModel;

@end

@implementation LWZFMRootViewControllerW

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"通知" message:@"你是傻逼吗!" delegate:self cancelButtonTitle:@"我是傻逼" otherButtonTitles:nil, nil];
    alertView.alertViewStyle = UIAlertViewStyleDefault;
    [alertView show];
    alertView.cancelButtonIndex = 1;

    
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.73 green:0.81 blue:0.96 alpha:1];
    [[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    self.view.backgroundColor = [UIColor grayColor];
    
    self.navigationController.navigationBar.translucent = NO;
    self.backImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
    self.backImageView.image = [UIImage imageNamed:@"backImageW"];
    [self.view addSubview:self.backImageView];
    
    [self creatTitleButton];
    [self createTableView];
    [self handleData];
}

- (void)creatTitleButton {

    self.titleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.titleButton setTitle:@"listFM" forState:UIControlStateNormal];
    self.titleButton.titleLabel.font = [UIFont fontWithName:@"Zapfino" size:20];
    [self.titleButton setTitleColor:[UIColor magentaColor] forState:UIControlStateNormal];
    [self.titleButton addTarget:self action:@selector(titleButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = self.titleButton;
}

- (void)titleButtonDidPress:(UIButton *)sender {
    NSLog(@"傻逼");
}

#pragma mark - 获取数据
- (void)handleData {
    
    [MHNetWorkTask getWithURL:kFMAllURL withParameter:nil withHttpHeader:nil withResponseType:ResponseTypeJSON withSuccess:^(id result) {
//            NSLog(@"%@", result);
        NSArray *datasArray = result[@"datas"];
        NSArray *datasArrayShow = [NSArray arrayWithObject:datasArray[0]];
        /** 节目榜单 */
        NSMutableArray *arrM1 = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dic in datasArrayShow) {
            self.listDatasModelShowModel = [[LWZFmListDatasModel alloc] initWithDataSource:dic];

            [arrM1 addObject:self.listDatasModelShowModel];
        }
        self.dataSourceShow = [NSMutableArray arrayWithArray:arrM1];
        
        NSArray *datasArrayHost = [NSArray arrayWithObject:datasArray[1]];
        /** 主播榜单 */
        for (NSDictionary *dic in datasArrayHost) {
//            NSLog(@"%@", self.listDatasModelHostModel.title);
            self.listDatasModelHostModel = [[LWZFmListDatasModel alloc] initWithDataSource:dic];
            [self.dataSourceHost addObject:self.listDatasModelHostModel];
        }
        
        /** 焦点图 */
        NSArray *focusImagesArray = [[result valueForKey:@"focusImages"] valueForKey:@"list"];
        for (NSDictionary *dic in focusImagesArray) {
            self.focusImagesModel = [[LWZFmFocusImagesListModel alloc] initWithDataSource:dic];
            NSLog(@"%@",self.focusImagesModel.pic);
            [self.dataSourceFocusImages addObject:self.focusImagesModel];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.tableView reloadData];
        });
        
    } withFail:^(NSError *error) {
        
    }];
}
#pragma mark - 创建TableView
- (void)createTableView {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 112) style:UITableViewStyleGrouped];
    
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.separatorColor = [UIColor greenColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    //注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [self.tableView registerClass:[LWZListCell class] forCellReuseIdentifier:@"LWZListCellShow"];
    [self.tableView registerClass:[LWZListCell class] forCellReuseIdentifier:@"LWZListCellHost"];
    
    [self.view addSubview:self.tableView];
}

#pragma mark - TableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 1) {

        return self.listDatasModelShowModel.listArray.count;
    }
    
    if (section == 2) {
        
        return self.listDatasModelHostModel.listArray.count;
    }else {
        return 0;
    }

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 1) {
        LWZListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LWZListCellShow"];
        cell.listModel = self.listDatasModelShowModel.listArray[indexPath.row];
        cell.backgroundColor = [UIColor clearColor];
        return cell;
    }
    if (indexPath.section == 2) {
        LWZListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LWZListCellHost"];
        cell.listModel = self.listDatasModelHostModel.listArray[indexPath.row];
        cell.backgroundColor = [UIColor clearColor];
        return cell;
    }else {
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}
#pragma mark - TbaleView点击时间
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    LWZListCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    LWZFMListViewController *listVC = [[LWZFMListViewController alloc] initWithName:cell.listModel.title withKeyURL:cell.listModel.key withContentTypeURL:cell.listModel.contentType];
    [self.navigationController pushViewController:listVC animated:YES];
}

#pragma mark - TableView的头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 200)];
        imageView.userInteractionEnabled = YES;
        [imageView yy_setImageWithURL:[NSURL URLWithString:self.focusImagesModel.pic] placeholder:nil];
        
        /** 添加手势 */
        UITapGestureRecognizer *tapGedtureRecongnizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGedtureRecongnizer:)];
        [imageView addGestureRecognizer:tapGedtureRecongnizer];
        
        return imageView;
    }
    if (section == 1) {
        UIView *view = [[UIView alloc] init];
        UILabel *labelColor = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 3, 16)];
        labelColor.backgroundColor = [UIColor redColor];
        [view addSubview:labelColor];
        
        UILabel *labelName = [[UILabel alloc] initWithFrame:CGRectMake(27, 2, 100, 16)];
        labelName.text = self.listDatasModelShowModel.title;
        labelName.font = [UIFont systemFontOfSize:13];
        [view addSubview:labelName];
        return view;
    }
    
    if (section == 2) {
        UIView *view = [[UIView alloc] init];
        UILabel *labelColor = [[UILabel alloc] initWithFrame:CGRectMake(20, 2, 3, 16)];
        labelColor.backgroundColor = [UIColor redColor];
        [view addSubview:labelColor];
        
        UILabel *labelName = [[UILabel alloc] initWithFrame:CGRectMake(27, 2, 100, 16)];
        labelName.text = self.listDatasModelHostModel.title;
        labelName.font = [UIFont systemFontOfSize:13];
        [view addSubview:labelName];
        return view;
    }
    return nil;
}

- (void)tapGedtureRecongnizer:(UITapGestureRecognizer *)sender {
    
    //    NSLog(@"%ld", tapGedtureRecongnizer.view.tag);
    LWZBannerViewController *banner = [[LWZBannerViewController alloc] initWithURL:self.focusImagesModel.url];
    //    [self presentViewController:banner animated:YES completion:nil];
    [self.navigationController pushViewController:banner animated:YES];
}

#pragma mark - Height for row
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 170;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (section == 0) {
        return 200;
    }else {
        return 20;
    }
    
}

#pragma mark - ------------------------------------
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
